package com.phpxiu.app.view.custom;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;

/**
 * Created by 吴金洲 on 2016/7/21
 */
@Deprecated
public class FlyWordsSwitchView extends LinearLayout implements Animator.AnimatorListener{
    private int mWidth;
    private TextView thumbView;
    private boolean checkState=false;
    private boolean isSwitching=false;
    private AnimatorSet switchAnimSet;
    private ObjectAnimator switchAnim;
    private int thumbWidth,thumbHeight;
    private int thumbMarginLeft,thumbMarginRight;
    private int colorTextOn,colorTextOff;
    private FlyWordsSwitchListener listener;
    public FlyWordsSwitchView(Context context) {
        super(context);
    }

    public FlyWordsSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public FlyWordsSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @TargetApi(21)
    public FlyWordsSwitchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlyWordsSwitch);
        thumbWidth=a.getDimensionPixelSize(R.styleable.FlyWordsSwitch_fwsThumbWidth,-1);
        thumbHeight=a.getDimensionPixelSize(R.styleable.FlyWordsSwitch_fwsThumbHeight,-1);
        thumbMarginLeft=a.getDimensionPixelSize(R.styleable.FlyWordsSwitch_fwsThumb_marginLeft,0);
        thumbMarginRight=a.getDimensionPixelSize(R.styleable.FlyWordsSwitch_fwsThumb_marginRight,0);
        colorTextOn=a.getColor(R.styleable.FlyWordsSwitch_fwsThumbTextColorOn,0);
        colorTextOff=a.getColor(R.styleable.FlyWordsSwitch_fwsThumbTextColorOff,0);
        a.recycle();
        LayoutParams layoutParams=new LayoutParams(thumbWidth,thumbHeight);
        layoutParams.setMargins(thumbMarginLeft,0,thumbMarginRight,0);
        layoutParams.gravity= Gravity.CENTER|Gravity.LEFT;
        thumbView=(TextView)LayoutInflater.from(context).inflate(R.layout.fly_words_switch_thumb_txt,null);
        addView(thumbView,layoutParams);
        switchAnimSet=new AnimatorSet();
        switchAnim=ObjectAnimator.ofFloat(thumbView,"translationX",0,0);
        switchAnimSet.play(switchAnim);
        switchAnimSet.setDuration(300);
        switchAnimSet.addListener(this);
        switchAnimSet.setInterpolator(new LinearInterpolator());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取本身的宽高 这里要注意,测量之后才有宽高
        mWidth = getMeasuredWidth();
    }

    /**
     * 切换状态
     */
    public void switchState(){
        if(isSwitching){
           return;
        }
        isSwitching=true;
        checkState=!checkState;
        int distance=mWidth-thumbMarginRight-thumbMarginLeft;
        if(checkState){//从关到开
            setBackgroundResource(R.drawable.fly_words_switch_on);
            thumbView.setTextColor(colorTextOn);
            switchAnim.setFloatValues();
            switchAnim.setFloatValues(0,distance-thumbWidth);
            if(listener!=null){
                listener.FlyWordsSwitchOn();
            }
        }else{//从开到关
            thumbView.setTextColor(colorTextOff);
            setBackgroundResource(R.drawable.fly_words_switch_off);
            switchAnim.setFloatValues(distance-thumbWidth,0);
            if(listener!=null){
                listener.FlyWordsSwitchOff();
            }
        }
        switchAnimSet.start();
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
            isSwitching=false;
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    public void addFlyWordsSwitchListener(FlyWordsSwitchListener l){
        listener=l;
    }

    /**
     * 开关对外接口
     */
    public interface FlyWordsSwitchListener {
        void FlyWordsSwitchOn();
        void FlyWordsSwitchOff();
    }
}
